Cricket.Live = {	
				Container: 'live-container',
				ListView: 'live-list-view',
				DetailsView: 'live-details-view',
				TabId: 'live',
				ParsedFeed: [],
				Data: {Success: 1, Message: ''},
				AllTeams: [],
				AllMonths: [],
				CurrentTeam: '',
				CurrentMonth: '',
				Reloaded: true,
				CurrentView: 'list',
				NextUpdate: '',
				UpdateInterval: 60000,
				AllUpdates: [],
				AllTimes: [],
				Timer:false,
				search: function() {
					var Results = Cricket.Live.ParsedFeed
					return Results;
				},
				updateCounters: function() {
					
					var CurrentDate = new Date();
					if(!Cricket.Live.NextUpdate) {
						Cricket.Live.NextUpdate = CurrentDate.getTime() + Cricket.Live.UpdateInterval;
					}
					var MilisecondsLeft = Cricket.Live.NextUpdate - CurrentDate.getTime();
					if(MilisecondsLeft < 0) {
						MilisecondsLeft = MilisecondsLeft + Cricket.Live.UpdateInterval;
					}
					var SecondsLeft = Math.round(MilisecondsLeft/1000);
					if(10 > SecondsLeft) {
						SecondsLeft = '0' + SecondsLeft;
					}
					
					for(var a = 0; a < Cricket.Live.AllUpdates.length; a++) {
						Cricket.Live.AllUpdates[a].innerHTML = SecondsLeft;
					}
					var TimeString = Cricket.Util.findClockLabel(CurrentDate);
					
					for(var t = 0; t < Cricket.Live.AllTimes.length; t++) {
						Cricket.Live.AllTimes[t].innerHTML = TimeString;
					}
					
				},
				load: function() {
					//create dom
					Cricket.Live.Reloaded = true;
					Cricket.Live.AllUpdates = document.getElementsByClassName('update-in-seconds');
					Cricket.Live.AllTimes = document.getElementsByClassName('current-time');
					if('list' == Cricket.Results.CurrentView && 0 < Cricket.Live.ParsedFeed.length && false == Cricket.Controller.NotificationClosed) {
						Cricket.Controller.showLiveNotification();
					} 
					//prepare AllTeams
					//Cricket.Live.AllTeams = Cricket.Util.loadAllTeams(this.ParsedFeed);
					//Cricket.Live.AllMonths = Cricket.Util.loadAllMonths(this.ParsedFeed);	
					//set date
					if(!this.Timer)
						this.Timer=setInterval(function(){Cricket.Live.updateCounters()}, 1000);
					var CurrentDate = new Date();
					_gel('current-date').innerHTML = Cricket.Util.findDayLabel(CurrentDate.getDay()) + ', ' + CurrentDate.getDate() + ' ' + Cricket.Util.findMonthLabel(CurrentDate.getMonth()) + ' ' + CurrentDate.getFullYear();
					
					//update live match count 
					_gel('live-match-count').innerHTML = this.ParsedFeed.length;				
					
					//show if current
					if(this.TabId == Cricket.Controller.getCurrentTab()) {
						this.show();
					}
				},
				show: function() {					
					Cricket.Controller.activateTab(this);
				},
				showList: function() {
					Cricket.Controller.hideCricBuzz();
					Cricket.Live.hideInners();
					Cricket.Live.CurrentView = 'list';
					_gel('live-list-view').style.display 				= 'block';
					_gel('live-list-header').style.display 				= 'block';
				},
				drawList: function() {
					var Results = this.search();
					//console.log(Results);
					var ListContainer = 'live-list-view';
					var ListView = document.createElement('ul');
					ListView.className = 'table-container';
					
					if(0 < Results.length) {
						var RowCounter = 0;
						for(var i = 0; i < Results.length; i++ ) {
							var ListItem = document.createElement('li');
							ListView.appendChild(ListItem);
							
							var HeaderDiv = document.createElement('div');
							HeaderDiv.className = 'table-header';
							ListItem.appendChild(HeaderDiv);
							
							var Left = document.createElement('div');
							Left.className = 'header-left';
							Left.innerHTML = Results[i].Teams[1].fullname;
							HeaderDiv.appendChild(Left);
							
							var DetailsLinkContainer = document.createElement('div');
							if(false !== Results[i].ScoreCard && 0 !== Results[i].ScoreCard.CurrentScores.BatTeam) {						
								DetailsLinkContainer.className = 'link-container-right';
								var DetailsLink = document.createElement('a');
								DetailsLink.href= '#';
								DetailsLink.className = 'stretch-anchor arrow-right';
								DetailsLink.onclick = Cricket.Util.generateDetailsToggle(Cricket.Live, Results[i].Id);
								DetailsLinkContainer.appendChild(DetailsLink);
							} else {
								DetailsLinkContainer.innerHTML = '&nbsp;';
								DetailsLinkContainer.className = 'right';
							}
							HeaderDiv.appendChild(DetailsLinkContainer);
							
							var Right = document.createElement('div');
							Right.className = 'header-right';
							Right.innerHTML = Results[i].Teams[2].fullname;
							HeaderDiv.appendChild(Right);
							
							HeaderDiv.appendChild(document.createTextNode('vs'));
							
							
							//INIT INFO
							CurrentInnings = 0;
							CurrentBatTeam = 0;
							CurrentRuns = 0;
							CurrentWickets = '';
							var LeftText = '&nbsp;';
							var RightText = '&nbsp;';
							
							if(false !== Results[i].ScoreCard && Results[i].ScoreCard.CurrentScores) {
								CurrentInnings  = parseInt(Results[i].ScoreCard.CurrentScores.InningsNo);
								CurrentBatTeam = Results[i].ScoreCard.CurrentScores.BatTeam;
								CurrentRuns = Results[i].ScoreCard.CurrentScores.BatTeamRuns;
								CurrentWickets = '/' + Results[i].ScoreCard.CurrentScores.BatTeamWickets;
							}
							var TableDiv = document.createElement('div');
							TableDiv.className = 'table';
							ListItem.appendChild(TableDiv);
							
							var Left = document.createElement('div');
							Left.className = 'left';
							
							if(false !== Results[i].ScoreCard && 0 <  Results[i].ScoreCard.Aggregate[1].InningsNum.length) {
								var Runs = [];
								for (var agin = 0; agin <  Results[i].ScoreCard.Aggregate[1].InningsNum.length; agin++) {
									if( Results[i].ScoreCard.Aggregate[1].InningsNum[agin] == CurrentInnings) {
										Runs.push( Results[i].ScoreCard.Innings[Results[i].ScoreCard.Aggregate[1].InningsNum[agin]].TotalRuns + CurrentWickets);
									} else {
										Runs.push( Results[i].ScoreCard.Innings[Results[i].ScoreCard.Aggregate[1].InningsNum[agin]].TotalRuns);
									}
								}
								LeftText = Runs.join(' & ');
							} 
							Left.innerHTML = LeftText;
							TableDiv.appendChild(Left);
							
							var Right = document.createElement('div');
							Right.className = 'right';
							if(false !== Results[i].ScoreCard && 0 <  Results[i].ScoreCard.Aggregate[2].InningsNum.length) {
								var Runs = [];
								for (var agin = 0; agin <  Results[i].ScoreCard.Aggregate[2].InningsNum.length; agin++) {
									if( Results[i].ScoreCard.Aggregate[2].InningsNum[agin] == CurrentInnings) {
										Runs.push( Results[i].ScoreCard.Innings[Results[i].ScoreCard.Aggregate[2].InningsNum[agin]].TotalRuns + CurrentWickets);
									} else {
										Runs.push( Results[i].ScoreCard.Innings[Results[i].ScoreCard.Aggregate[2].InningsNum[agin]].TotalRuns);
									}
								}
								RightText = Runs.join(' & ');
							}
							
							Right.innerHTML = RightText;
							TableDiv.appendChild(Right);
							
							var Middle = document.createElement('div');
							Middle.className = 'middle';
							Middle.innerHTML = Results[i].Series;
							TableDiv.appendChild(Middle);			
	
						}
					} else {
						var ListItem = document.createElement('li');
						ListView.appendChild(ListItem);
						
						var Message = document.createElement('div');
						Message.className = 'message';
						Message.innerHTML = 'There are no live matches';
						ListItem.appendChild(Message);
					}
					
					//load list
					_gel(this.ListView).innerHTML = '';
					_gel(this.ListView).appendChild(ListView); 
				},
				drawDetails: function(MatchIndex) {
					//hide loader
					var GeneralContainer = _gel('live-details-general');
					GeneralContainer.innerHTML = '';
					var ScorecardContainer = _gel('live-details-scorecard');
					ScorecardContainer.innerHTML = '';
					
					if(-1 !== MatchIndex) {
						var Match = Cricket.Live.ParsedFeed[MatchIndex];
						if(typeof Match == 'object') {
					
							//GENERAL
							//header
							var GeneralHeaderContainer = _gel('live-details-general-header');
							GeneralHeaderContainer.innerHTML = '';
							
							GeneralHeader_Main = document.createElement('div');
							GeneralHeader_Main.className = 'small-white-text';
							GeneralHeaderContainer.appendChild(GeneralHeader_Main);
									
							//Scorecard link
							var Right = document.createElement('div');
							Right.className = 'right';
							GeneralHeader_Main.appendChild(Right);
							
							var ScoreCardContainer = document.createElement('div');
							ScoreCardContainer.className = 'link-container-right';
							Right.appendChild(ScoreCardContainer);
							
							var ScoreCardLink = document.createElement('a');
							ScoreCardLink.href='#';
							ScoreCardLink.className = 'stretch-anchor arrow-right';
							ScoreCardLink.onclick = Cricket.Live.generateShowDetailsSocreCard(parseInt(Match.ScoreCard.CurrentScores.InningsNo));
							ScoreCardContainer.appendChild(ScoreCardLink);
							
							Right.appendChild(document.createTextNode('Scorecard'));
									
							//middle 
							var Middle = document.createElement('div');
							Middle.className = 'middle';
							Middle.appendChild(document.createTextNode(Match.Teams[1].shortname + ' vs ' + Match.Teams[2].shortname + ' '));
							var CurrentTimeSpan = document.createElement('span');
							CurrentTimeSpan.className = 'current-time';
							Middle.appendChild(CurrentTimeSpan);
							var TextSpan = document.createElement('span');
							TextSpan.innerHTML = ' Update in: 0:';
							Middle.appendChild(TextSpan);							
							var UpdateSpan = document.createElement('span');
							UpdateSpan.className = 'update-in-seconds';
							Middle.appendChild(UpdateSpan);
							GeneralHeader_Main.appendChild(Middle);
							
							Cricket.Live.AllUpdates = document.getElementsByClassName('update-in-seconds');
							Cricket.Live.AllTimes = document.getElementsByClassName('current-time');
							
							//list link
							var Left = document.createElement('div');
							Left.className = 'left';
							GeneralHeader_Main.appendChild(Left);
							
							var ListLinkContainer = document.createElement('div');
							ListLinkContainer.className = 'link-container';
							Left.appendChild(ListLinkContainer);
							
							var ListLink = document.createElement('a');
							ListLink.href='#';
							ListLink.className = 'stretch-anchor arrow-left';
							ListLink.onclick = Cricket.Util.generateShowList(Cricket.Live);
							ListLinkContainer.appendChild(ListLink);
							
							Left.appendChild(document.createTextNode('Live'));
							GeneralHeader_Main.appendChild(Left);
							
							//general info
							var GeneralDiv = document.createElement('div');
							if(0 !== Match.ScoreCard.CurrentScores.BatTeam) {
								var GeneralTopLeft = document.createElement('div');
								GeneralTopLeft.id = 'live-details-top-left';
								GeneralDiv.appendChild(GeneralTopLeft);
								
								var BatTeamContainer =  document.createElement('div');
								GeneralTopLeft.appendChild(BatTeamContainer);
								
								var Large = document.createElement('div');
								Large.className = 'large';
								Large.innerHTML = Match.Teams[Match.ScoreCard.CurrentScores.BatTeam].fullname;
								BatTeamContainer.appendChild(Large);
								
								var Small = document.createElement('div');
								Small.className = 'small';
								//TODO
								var ResultString = '';
								switch(Match.Type) {
									case 'ODI':
										if('' != Match.ScoreCard.CurrentScores.Target) {
											ResultString = ' is chasing ' + Match.ScoreCard.CurrentScores.Target;
										}
										break;
									case 'TEST':
										if('' != Match.ScoreCard.CurrentScores.Target && '' != Match.ScoreCard.CurrentScores.BatTeamRuns) {
											ResultString = ' is ' + (parseInt(Match.ScoreCard.CurrentScores.Target) - parseInt(Match.ScoreCard.CurrentScores.BatTeamRuns)) + ' runs behind ';
										}
										break;
								}
								Small.innerHTML = ResultString;
								BatTeamContainer.appendChild(Small);
								
								var InningsContainer = document.createElement('div');
								GeneralTopLeft.appendChild(InningsContainer);
								
								var Large = document.createElement('div');
								Large.className = 'large';
								Large.innerHTML = Match.ScoreCard.CurrentScores.BatTeamOvers + ' overs';
								InningsContainer.appendChild(Large);
								
								var Small = document.createElement('div');
								Small.className = 'small';
								var InningsString = 'Innings:' + Match.ScoreCard.CurrentScores.InningsNo +' (Runrate:'+Match.ScoreCard.CurrentScores.CurrentRunRate;
								if(0 !== Match.ScoreCard.CurrentScores.RequiredRunRate) {
									InningsString += ' Required:'+Match.ScoreCard.CurrentScores.RequiredRunRate+')';
								} else {
									InningsString += ')';
								}
								Small.innerHTML = InningsString;
								InningsContainer.appendChild(Small);
								
								var GeneralTopRight = document.createElement('div');
								GeneralTopRight.id = 'live-details-top-right';
								GeneralDiv.appendChild(GeneralTopRight);
								
								var Large = document.createElement('div');
								Large.className = 'large';
								Large.innerHTML = Match.ScoreCard.CurrentScores.BatTeamRuns + '/' + Match.ScoreCard.CurrentScores.BatTeamWickets;
								GeneralTopRight.appendChild(Large);
								
								var Small = document.createElement('div');
								Small.className = 'small';
								Small.innerHTML = 'Extras: ' + Match.ScoreCard.CurrentScores.Extras.Total;
								GeneralTopRight.appendChild(Small);		
							} else {
								GeneralDiv.appendChild(document.createTextNode('Information currently unavailable'));
							}
							
							GeneralContainer.appendChild(GeneralDiv);
							
							//table container
							var TableContainer = document.createElement('ul');
							TableContainer.className = 'table-container';
							GeneralContainer.appendChild(TableContainer);
							
							//Batsment
							var BatsmenLi = document.createElement('li');
							TableContainer.appendChild(BatsmenLi);
							
							var BmHeader = document.createElement('div');
							BmHeader.className = 'small-green-header';
							BmHeader.innerHTML = 'Batsmen';
							BatsmenLi.appendChild(BmHeader);
							
							var Bm = document.createElement('table');
							Bm.className = 'full-width-table';
							Bm.id = 'live-scorecard-batsmen';
							BatsmenLi.appendChild(Bm);
							
							var BmBody = document.createElement('tbody');
							Bm.appendChild(BmBody);
							for(var bt = 0; bt < Match.ScoreCard.CurrentScores.BatsMen.length; bt++) {
								if('' != Match.ScoreCard.CurrentScores.BatsMen[bt].FullName) {
									var BtRow = document.createElement('tr');
									BtRow.className = (0 == bt%2) ? 'row-dark normal-white-text' : 'row-light normal-white-text';
									BmBody.appendChild(BtRow);
									
									//name
									BtName = document.createElement('td');
									BtName.className = 'cell-1';
									BtName.innerHTML = Match.ScoreCard.CurrentScores.BatsMen[bt].FullName;
									BtRow.appendChild(BtName);
									
									//balls
									BtBalls = document.createElement('td');
									BtBalls.className = 'cell-2';
									BtBalls.innerHTML = 'Balls: ' +Match.ScoreCard.CurrentScores.BatsMen[bt].Balls;
									BtRow.appendChild(BtBalls);
									
									//4s
									BtFours = document.createElement('td');
									BtFours.className = 'cell-3';
									BtFours.innerHTML = '4s: ' +Match.ScoreCard.CurrentScores.BatsMen[bt].Fours;
									BtRow.appendChild(BtFours);
									
									//6s
									BtSixes = document.createElement('td');
									BtSixes.className = 'cell-4';
									BtSixes.innerHTML = '6s: ' +Match.ScoreCard.CurrentScores.BatsMen[bt].Sixes;
									BtRow.appendChild(BtSixes);
									
									//Runs
									BtRuns = document.createElement('td');
									BtRuns.className = 'cell-last';
									BtRuns.innerHTML = Match.ScoreCard.CurrentScores.BatsMen[bt].Runs;
									BtRow.appendChild(BtRuns);
									
									var BtSpacer = document.createElement('tr');
									BtSpacer.className = 'spacer';
									BmBody.appendChild(BtSpacer);
								}
							}
							
							//Bowlers
							var BwLi = document.createElement('li');
							TableContainer.appendChild(BwLi);
							
							var BwHeader = document.createElement('div');
							BwHeader.className = 'small-green-header';
							BwHeader.innerHTML = 'Bowlers';
							BwLi.appendChild(BwHeader);
							
							var Bw = document.createElement('table');
							Bw.className = 'full-width-table';
							Bw.id = 'live-scorecard-bowlers';
							BwLi.appendChild(Bw);
							
							var BwBody = document.createElement('tbody');
							Bw.appendChild(BwBody);

							for(var bw = 0; bw < Match.ScoreCard.CurrentScores.Bowlers.length; bw++) {
								if('' !=  Match.ScoreCard.CurrentScores.Bowlers[bw].FullName) {
									var BwRow = document.createElement('tr');
									BwRow.className = (0 == bt%2) ? 'row-dark normal-white-text' : 'row-light normal-white-text';
									BwBody.appendChild(BwRow);
									
									//name
									BwName = document.createElement('td');
									BwName.className = 'cell-1';
									BwName.innerHTML = Match.ScoreCard.CurrentScores.Bowlers[bw].FullName;
									BwRow.appendChild(BwName);
									
									//maidens
									BwMaidens = document.createElement('td');
									BwMaidens.className = 'cell-2';
									BwMaidens.innerHTML = Match.ScoreCard.CurrentScores.Bowlers[bw].Maidens +'m';
									BwRow.appendChild(BwMaidens);
									
									//Runs
									BwRuns = document.createElement('td');
									BwRuns.className = 'cell-3';
									BwRuns.innerHTML = Match.ScoreCard.CurrentScores.Bowlers[bw].Runs +'r';
									BwRow.appendChild(BwRuns);
									
									//Wickets
									BwWickets = document.createElement('td');
									BwWickets.className = 'cell-4';
									BwWickets.innerHTML = Match.ScoreCard.CurrentScores.Bowlers[bw].Wickets +'w';
									BwRow.appendChild(BwWickets);
									
									//Overs
									BwOvers = document.createElement('td');
									BwOvers.className = 'cell-last';
									BwOvers.innerHTML = Match.ScoreCard.CurrentScores.Bowlers[bw].Overs +'o';
									BwRow.appendChild(BwOvers);
									
									var BwSpacer = document.createElement('tr');
									BwSpacer.className = 'spacer';
									BwBody.appendChild(BwSpacer);
								}
							}
							
							//Last Wicket
							var LwLi = document.createElement('li');
							TableContainer.appendChild(LwLi);
							var LwContainer = document.createElement('div');
							LwContainer.className = 'small-green-header';
							LwContainer.innerHTML = 'Last wicket: ' + Match.ScoreCard.CurrentScores.LastWicket.FullName;
							LwLi.appendChild(LwContainer);
							TableContainer.appendChild(LwLi);
							
							//SCORE CARD
							Cricket.Util.drawScoreCard(Match, ScorecardContainer, 'live');
							
							if(Match.MoreInfo && '' != Match.MoreInfo) {
								Cricket.Controller.updateCricBuzz(Match.MoreInfo);
							}
							
							Cricket.Live.showDetailsGeneral();	
						}		
					} else {
						Cricket.Messages.show('Match could not be found');
					}
				},
				
				generateShowDetailsSocreCard: function(PreSelected) {
					return function() {
						Cricket.Live.showDetailsSocreCard(PreSelected);
						return false;
					}
				},
				showDetailsGeneral: function() {
					Cricket.Live.hideInners();
					Cricket.Live.CurrentView 							= 'details';
					_gel('live-details-view').style.display 			= 'block';
					_gel('live-details-general-header').style.display 	= 'block';
					_gel('live-details-general').style.display 			= 'block';
				},
				
				showDetailsSocreCard: function(PreSelected) {
					Cricket.Live.CurrentView 							= 'details';
					Cricket.Live.hideInners();
					_gel('live-details-view').style.display 			= 'block';
					_gel('live-details-scorecard-header').style.display = 'block';
					_gel('live-details-scorecard').style.display 		= 'block';
					//select the correct one
					for(var o = 0; o < _gel('live-scorecard-dropdown').options.length; o++) {
						if(_gel('live-scorecard-dropdown').options[o].value == PreSelected) {
							_gel('live-scorecard-dropdown').options[o].selected = 'selected';
							break;
						}
					}
					Cricket.Live.showInnings(PreSelected);
				},
				
				showInnings: function(InningsNum) {
					//hide all
					AllInnings = _gel('live-details-scorecard').getElementsByTagName('ul');
					for(var i=0; i<AllInnings.length; i++) {
						AllInnings[i].style.display = 'none';
					}
					_gel('live-details-scorecard-innings-'+InningsNum).style.display = 'block';
				},
				
				hideInners: function() {
					_gel('live-list-view').style.display 				= 'none';
					_gel('live-list-header').style.display 				= 'none';
					_gel('live-details-view').style.display 			= 'none';
					_gel('live-details-general').style.display 			= 'none';
					_gel('live-details-scorecard').style.display 		= 'none';
					_gel('live-details-general-header').style.display 	= 'none';
					_gel('live-details-scorecard-header').style.display = 'none';
				},
				
				loadError: function() {
					Cricket.Live.Data.Success = 0;
					Cricket.Live.Data.Message = 'Information on Live Matches is currently unavailable';
					//show if current
					if(this.TabId == Cricket.Controller.getCurrentTab()) {
						this.show();
					} 
				}
			}